package me.zhenqu.redis.utils;

import java.io.UnsupportedEncodingException;

/**
 * Created by IntelliJ IDEA.
 * User: yibing.tan
 * Date: 11-11-14
 * Time: 下午4:20
 * To change this template use File | Settings | File Templates.
 */
public class StringCoder extends CacheCoder<String> {
    public static StringCoder INST = new StringCoder();

    @Override
    public byte[] encode(String o) {
        if (o == null) return new byte[0];
        try {
            return o.getBytes("utf-8");
        } catch (UnsupportedEncodingException ignored) {
            return null;
        }
    }

    @Override
    public String decode(byte[] val, int offset, int size) {
        try {
            return new String(val, offset, size, "utf-8");
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }
}
